package com.example.dw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dw.entity.CourseComment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 课程评论Mapper接口
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface CourseCommentMapper extends BaseMapper<CourseComment> {

    /**
     * 根据课程ID获取评论（带用户信息）
     */
    @Select("SELECT cc.*, u.username, u.nickname, u.avatar " +
            "FROM course_comment cc " +
            "LEFT JOIN user u ON cc.userid = u.userid " +
            "WHERE cc.courseid = #{courseid} " +
            "ORDER BY cc.createdAt DESC")
    List<CourseComment> getCourseComments(@Param("courseid") Integer courseid);

    /**
     * 根据课程ID分页获取评论（带用户信息）
     */
    @Select("SELECT cc.*, u.username, u.nickname, u.avatar " +
            "FROM course_comment cc " +
            "LEFT JOIN user u ON cc.userid = u.userid " +
            "WHERE cc.courseid = #{courseid} " +
            "ORDER BY cc.createdAt DESC " +
            "LIMIT #{offset}, #{size}")
    List<CourseComment> getCourseCommentsByPage(@Param("courseid") Integer courseid,
                                                @Param("offset") Integer offset,
                                                @Param("size") Integer size);

    /**
     * 根据课程ID获取评论总数
     */
    @Select("SELECT COUNT(*) FROM course_comment WHERE courseid = #{courseid}")
    Integer getCourseCommentCount(@Param("courseid") Integer courseid);

    /**
     * 根据课程ID获取平均评分
     */
    @Select("SELECT AVG(score) FROM course_comment WHERE courseid = #{courseid}")
    Double getAverageScoreByCourseId(@Param("courseid") Integer courseid);
} 